/**
  ETFAna project, Anyang Normal University && IMP-CAS
  \class ETFUserAnaTask
  \brief user class for tasks in the tracking stage
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-10-31
  \date 2021-12-03 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                    </tr>
  <tr>  <td>2021-10-31   <td>Asia Sun    <td>file created                   </tr>
  </table>

  \copyright Copyright (c) 2021-2024 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#ifndef ETFUserAnaTask_h
#define ETFUserAnaTask_h

#include "ETFTask.h"

class TH1F;
class TH2F;
class TTree;
class ETFTimer2;
class ETFTimer4;
class ETFTOFWall;
class ETFT0Array;
class ETFMUSIC;
class ETFDCTa;
class ETFDC;

class ETFUserAnaTask : public ETFTask{
public:
  ETFUserAnaTask(const char *name = "UserAnaTask");
  virtual ~ETFUserAnaTask();

  virtual void Exec() override;
  virtual void Configure();
  virtual void Reset();

  /// \param x,y: aoz0, z0
  static double z0Straight(double x, double y); ///< make z0 straight w.r.t aoz0
  /// \param x,y: aoz0, z0
  static double aoz0Straight(double x, double y); ///< give aoz0 physical meaning
  void SetBrho0(double br){ fBrho0 = br; }

  // ClassDefOverride(ETFUserAnaTask, 1); // user class for tasks in the tracking stage

protected:
  virtual void CreateHistos() override;
  virtual void WriteHistos() override;

  double fBrho0; // brho in RIBLL2
  TTree *et; ///< the et tree - simpler, yet succinct
  double x_f1, t0_f1, t0arr_m; ///< f1 pos, t0 time (4 ends), and time of t0arr
  double tof1, tof1_v, tof2; // tof1_v: tof1 measured by VME
  double beta0, bg0, brho0, aoz0; // pid in ribll2
  double dE[2];
  double z0, z1;
  double aoz0c, z0c; // corrected aoz0 and a0c - by violence
  // tracking related //
  double k0[2], b0[2]; // preTa
  double k1[2], b1[2]; // postTa
  double k2[2], b2[2]; // postMag
  // hit position [2]: [x-y] at z of x0 wire plane for each dc
  double pT1[2], pTAU[2], pTAD[2]; // U(D): from tracking of dcTaU(D) array
  double pdcTaU[2][2], pdcTaD[2][2]; // [U,D][x-y]
  double pdcTaWire[2][2][2]; // [U-D][U(D)0-1][x-y] - hitpos, but only using wire pos
  double pdc[3][2], ptofw[2]; // dc[0-1-2][x-y]
  double rho, beta, bg, aoz;
  // match window: 46us
  double fPileUpT; ///< the hit closest to trigger-generating particle
  int fPileUpN; ///< nof hits of tstop

  //----- some histos -----//
  // T0 and T1 statistics -- for the main PXI and VME daq alignment
  TH2F *hT0DtPvV; ///< tstart - PXI v.s. VME
  TH2F *hT1DtPvV; ///< tstop - PXI v.s. VME
  TH2F *hT0LR, *hT1LR; ///< L:R: for vme only
  TH2F *hT0LE, *hT0RE; ///< L : # of edges: tstart, for vme only
  TH2F *hT1LE, *hT1RE; ///< L : # of edges: tstop, for vme only
  TH1F *hT0L, *hT0R, *hT1L, *hT1R; ///< projection of hT0(1)L(R)E to the Y axis
  // PID relataed histos
  TH1F *ht0arrMulti, *htofwMulti; ///< ETFTOFWall multiplicity (fired() == 4)
  TH1F *hx_f1, *htofwDis; ///< fired distribution of tofw and t0arr
  TH1F *htof1, *htof1_v;
  TH2F *htof1p_v, *htof2, *htofwDT; ///< tof2: from tstart to tstop, DT: tu-td
  TH1F *hdE0, *hdE1;
  TH2F *htof2_dE1, *htof1_v_dE0, *htof1_v_dE1, *htof1_dE0, *htof1_dE1; ///< tof-deltaE spectra
  // F1 related specta
  TH2F *ht04ud_lr, *hx04ud_lr, *hlr_t02_t04;
  TH2F *hx_f1_lr, *hx_f1_ud, *hxf1_xETF, *hx_f1_tof1;

  // multiplicities and position distributions //
  TH1F *hmdc[3][6], *hpdc[3][6]; // [X-U-V][dc0-x0-1--dc1--dc2]
  TH1F *hmdcTa[2][2][4], *hpdcTa[2][2][4]; // [U-D][X-Y][dcTaU(D)0-x0-1--dcTaU(D)1]
  TH2F *hm2dcTa[2][2], *hp2dcTa[2][2]; // [U-D][dcTaU(D)0-1] - the 2D versions
  // tracking related //
  TH1F *htdc[3][6]; // [X-U-V][dc0-x0-1--dc1--dc2]
  TH1F *htdcTa[2][2][4]; // [U-D][X-Y][dcTaU(D)0-x0-1--dcTaU(D)1]
  TH1F *haTa[2][2], *hadcx; // angle in deg: [U-D][X-Y]
  TH2F *hTAp[2], *hVETO; // beam spot on the target: [U-D], veto to indicate offTA beam
  // PID related //
  TH1F *hz[2]; // [preTA-postTA]
  TH2F *hpid0; // z0-aoz0

  // some detectors //
  ETFTimer2 *fT1p, *fT1v, *fT0p, *fT0v; ///< tstop and tstart of PXI and VME
  ETFTimer4 *fT0F1, *fVETO; // tstart at F1, and VETO before tstop
  ETFTOFWall *fTOFW;
  ETFT0Array *fT0Arr; ///< just as its name indicates
  ETFMUSIC *fMU[2]; ///< [2]: the music pre-([0]) and post-([1]) the reaction target
  ETFDCTa *fdcTa[2][2];
  ETFDC *fdc[3];
};

#endif
